"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 导入导出支持M* 特性
Case Name   : 导入时参数-i和-t的使用
Create At   : 2023.09.12
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.建表并创建索引
    3.导出tar格式文件
    4.导出之前的数据到新库，指定-t和-i选项
    5.连接导入库查询
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功，表1索引导入成功，表2导入成功但无索引
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.dbuser_node = Node('dbuser')
        self.commonsh = CommonSH('dbuser')
        self.db_name = 'db_tool0039'
        self.tb_name = 'tb_tool0039'
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0039')

    def test_server_tools1(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1:创建兼容b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name}_01;" \
                  f"create database {self.db_name}_01 dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败:' + text)

        text = '----step2:建表并创建索引;expect:成功----'
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(f'''drop table if exists \
        {self.tb_name},{self.tb_name}_01;
        create table {self.tb_name} (
        id int not null auto_increment,
        name varchar(50) not null,
        age int not null,
        primary key (id),
        index idx_name (name),
        index idx_age (age)
        );           
        create table {self.tb_name}_01 (id  int,name char(20),\
        key idx_id(id));''', dbname=self.db_name)
        self.log.info(sql_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_cmd,
                      '执行失败:' + text)

        text = '---step3:导出tar格式文件;expect:成功-----------'
        self.log.info(text)
        dump_cmd = f"source {macro.DB_ENV_PATH};" \
                   f"gs_dump -p {self.dbuser_node.db_port} " \
                   f" {self.db_name}  " \
                   f"-f {self.dump_path} -F t"
        self.log.info(dump_cmd)
        dump_msg = self.dbuser_node.sh(dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, dump_msg,
                      '执行失败:' + text)

        text = '---step4:导出之前的数据到新库;expect:成功-----------'
        self.log.info(text)
        restore_cmd = f"source {macro.DB_ENV_PATH} ;" \
                      f"gs_restore -p {self.dbuser_node.db_port} " \
                      f"-d {self.db_name}_01 " \
                      f"{self.dump_path}  " \
                      f"-t {self.tb_name} " \
                      f"-t {self.tb_name}_01 " \
                      f"-I idx_name -I idx_age"
        self.log.info(restore_cmd)
        restore_msg = self.dbuser_node.sh(restore_cmd).result()
        self.log.info(restore_msg)
        self.assertIn('restore operation successful', restore_msg)

        text = '-step5:连接导入库查询;expect:成功，表1索引导入成功，表2导入成功但无索引-'
        self.log.info(text)
        sql_cmd = f"show create table {self.tb_name};" \
                  f"show create table {self.tb_name}_01;"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=f'{self.db_name}_01')
        self.log.info(sql_result)
        self.assertIn('CREATE INDEX idx_name', sql_result, '执行失败:' + text)
        self.assertIn('CREATE INDEX idx_age', sql_result, '执行失败:' + text)
        self.assertNotIn('idx_id', sql_result, '执行失败:' + text)

    def tearDown(self):
        text = '---step6:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name}_01;"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
